<template>
	<view class="busines-mod-list-top" v-if="listData.length">
		<scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="false"
			:scroll-into-view="scrollId">
			<view v-for="(tab,index) in listData" :key="index" class="uni-tab-item" :id="index" :data-current="tab"
				@click="clickLeftItem(index)">
				<text class="uni-tab-item-title"
					:class="tabIndex== index ? 'uni-tab-item-title-active' : ''">{{tab.nf}}年</text>
			</view>
		</scroll-view>
	</view>
	<view class="busines-mod-list" v-if="listData.length">
		<scroll-view scroll-y="true" :scroll-into-view="scrollId" scroll-with-animation @scroll="scrollEvt">
			<view class="list-item" v-for="(item,index) in listData" :key="item.nf" :id="'item'+index">
				<view v-for="itm in item.children" :key="itm.id">
					<uni-card :is-shadow="false" padding="0" spacing="0" margin="6" :key="index">
						<view class="busines-mod-list-title">
							<text>{{itm.zwyf+''}}</text>
						</view>
						<uni-list :border="false">
							<uni-list-item title="合计电量" :rightText="itm.hjdl + ''" />
							<uni-list-item title="应收电费" :rightText="moneyUtils(itm.ysdf)+ ''" />
							<uni-list-item title="欠费金额" :rightText="moneyUtils(itm.qfje)+ ''" />
							<!-- <uni-list-item title="备注" :rightText="itm.bzsm+ ''"/> -->
						</uni-list>
					</uni-card>
				</view>
			</view>
		</scroll-view>
	</view>
	<cNoData v-if="listData.length < 1" />
</template>

<script setup lang="ts">
	import cNoData from "@/pages/bspsd/components/no-data.vue";
	import { getDfzdByYhbh } from "@/api/request/getDfzdByYhbh";
	import { moneyUtils } from "@/utils/index";
	import { onBeforeMount, watch,  ref } from 'vue';
	const props = defineProps({
		yhbh: {
			type: String,
			default: ""
		}
	})
	let tabIndex = ref(0)
	let scrollId = ref("")
	let distanceList = ref([])
	let timer = ref(null)
	let isLeftClick = ref(false)
	let listData = ref([]);


	onBeforeMount(() => {
		getListData()
	})
	watch(() => props.yhbh, ()=>{
		getListData()
	})
	function getListData() {
		if(props.yhbh){
			getDfzdByYhbh( {
				data : {
					yhbh: props.yhbh,
				},
				success: (res) => {
					listData.value = []
					let listArr = res || []
					listData.value = listArr.reduce((result, item) => {
						let index = result.findIndex((it) => it.nf == item.nf);
						if (index == -1) {
							result.push({
								nf: item.nf,
								children: [item]
							});
						} else {
							result[index].children.push(item);
						}
						return result;
					}, []);
				},
				fail: (code, message) => {
					console.log(code, message)
				}
			})
		}
		
	}

	setTimeout(() => {
		getDistanceToTop();
	}, 500)

	function clickLeftItem(index : number) {
		isLeftClick.value = true
		tabIndex.value = index
		scrollId.value = 'item' + index
	}
	function getDistanceToTop() { //获取右侧各部分距离顶部的距离
		let selectorQuery = uni.createSelectorQuery()
		selectorQuery.selectAll('.list-item').boundingClientRect(function (rects) {
			rects.forEach(function (rect) {
				distanceList.value.push(rect.top)
			})
		}).exec()
	}

	// 元素滚动到顶部时，对应的 导航栏变为选中状态
	function scrollEvt(e) {
		// 点击导航栏引起的滚动不做判断
		if (isLeftClick.value) {
			isLeftClick.value = false
			return
		}
		// 防抖
		if (timer.value) {
			clearTimeout(timer.value)
		}
		timer.value = setTimeout(() => {
			let scrollTop = e.detail.scrollTop //滚动的高度
			// 找到位于顶部元素的索引，距离大于滚动高度的第一个元素的上一个元素就是此时位于顶部的元素
			let index = distanceList.value.findIndex(it => {
				// 滚动条的位置大于元素距离顶部位置的距离时，说明元素已经滑过了顶部
				return (it > scrollTop)
			}) - 1
			if (index == tabIndex.value) return
			tabIndex.value = index
		}, 200)
	}
</script>
<style lang="scss" scoped>
</style>